package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysUser;
import cn.stylefeng.guns.modular.yingbao.entity.UserIndent;
import cn.stylefeng.guns.modular.yingbao.entity.UserMy;
import cn.stylefeng.guns.modular.yingbao.entity.VipPay;
import cn.stylefeng.guns.modular.yingbao.mapper.SysUserMapper;
import cn.stylefeng.guns.modular.yingbao.mapper.UserIndentMapper;
import cn.stylefeng.guns.modular.yingbao.mapper.UserMyMapper;
import cn.stylefeng.guns.modular.yingbao.mapper.VipPayMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.UserIndentParam;
import cn.stylefeng.guns.modular.yingbao.model.result.UserIndentResult;
import  cn.stylefeng.guns.modular.yingbao.service.UserIndentService;
import cn.stylefeng.guns.modular.yingbao.service.UserMyService;
import cn.stylefeng.guns.modular.yingbao.util.DateUtils;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhyd.oauth.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.ws.Response;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户付款表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2019-12-16
 */
@Service
public class UserIndentServiceImpl extends ServiceImpl<UserIndentMapper, UserIndent> implements UserIndentService {

    @Resource
    UserMyMapper userMyMapper;
    @Resource
    VipPayMapper vipPayMapper;
    @Resource
    SysUserMapper sysUserMapper;

    @Override
    public void add(UserIndentParam param){
        UserIndent entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(UserIndentParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(UserIndentParam param){
        UserIndent oldEntity = getOldEntity(param);
        UserIndent newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public UserIndentResult findBySpec(UserIndentParam param){
        return null;
    }

    @Override
    public List<UserIndentResult> findListBySpec(UserIndentParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(UserIndentParam param){
        Page pageContext = getPageContext();
        param.setStatus(0);
        IPage status = this.baseMapper.selectPage(pageContext, new QueryWrapper<UserIndent>().eq("status", "0"));
        return LayuiPageFactory.createPageInfo(status);
    }

    @Override
    public ResponseData addIndent(String myuserid,String pic,int payeeid,int backup1,int backup3) {
        if(StringUtils.isEmpty(myuserid) || StringUtils.isEmpty(pic) || payeeid == 0 || backup1 == 0 || backup3 == 0){
            return ResponseData.error("参数错误");
        }
        if(!myuserid.equals(String.valueOf(backup1))){
            return ResponseData.error(500,"和您的会员账号不对应");
        }
        List<UserIndent> userIndents = this.baseMapper.selectList(new QueryWrapper<UserIndent>().eq("myuserid", myuserid).eq("status", 0));
        if(userIndents.size() > 0){
           return ResponseData.error(500,"您有正在审核的会员记录");
        }
        UserIndent userIndent = new UserIndent();
        userIndent.setBackup1(String.valueOf(backup1));
        userIndent.setBackup3(backup3);
        userIndent.setPic(pic);
        userIndent.setMyuserid(Long.parseLong(myuserid));
        userIndent.setPayeeid(payeeid);
        userIndent.setStatus(0);
        userIndent.setVersion(1);
        userIndent.setBackup2("0");
        userIndent.setBackup4(0);
        int insert = this.baseMapper.insert(userIndent);
        if (insert > 0) {
            return ResponseData.success();
        }
        return ResponseData.error(500, "添加失败");
    }

    @Override
    public ResponseData queryAll(String status) {
        //List<UserIndent> list = new ArrayList<>();
        List<UserIndent> list;
        if("3".equals(status)){
            list = this.baseMapper.selectList(new QueryWrapper<>());
        }else if("0".equals(status) || "1".equals(status)) {
            list = this.baseMapper.selectList(new QueryWrapper<UserIndent>().eq("status", status));
        }else{
            list = new ArrayList<>();
        }
        if(list.size() > 0){
            return ResponseData.success(list);
        }
        return ResponseData.error(500,"暂无记录");
    }

    /*@Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData upStatus(String id,String status) {
        Integer x = 0;
        UserIndent userIndent = this.baseMapper.selectById(id);
        if(userIndent == null){
            return ResponseData.error(500,"暂无此条消息");
        }
        Integer version = userIndent.getVersion();
        if("2".equals(status)){
            userIndent.setStatus(Integer.parseInt(status));
            userIndent.setVersion(userIndent.getVersion()+1);
            //贾儿子的
            x = this.baseMapper.update(userIndent,new QueryWrapper<UserIndent>().eq("id",id).eq("version",version));
            if(x > 0){
                return ResponseData.success();
            }
            return ResponseData.error(500,"刷新一下试试");
        }else if("1".equals(status)){
            UserMy userMy1 = userMyMapper.selectOne(new QueryWrapper<UserMy>().eq("id", userIndent.getMyuserid()));
            if(userMy1 == null){
                ResponseData.error(500,"暂无此人");
            }
            System.out.println();
            if(1 == userMy1.getVipGrade()){
                userIndent.setStatus(Integer.parseInt(status));
                userIndent.setVersion(userIndent.getVersion()+1);
                UserMy userMy = userMyMapper.selectById(userIndent.getMyuserid());
                if(userMy == null){
                    return ResponseData.error(500,"暂无此人");
                }

                VipPay vipPay = vipPayMapper.selectById(userIndent.getBackup3());
                if(vipPay == null){
                    return ResponseData.error(500,"未找到会员信息");
                }
                Date date = DateUtils.addDateMinut(userMy.getVipTime(), (vipPay.getDay() * 24));
                userMy.setVipTime(date);
                userMy.setVipDuration(vipPay.getDay()+userMy.getVipDuration());
                Integer version1 = userMy.getVersion();
                userMy.setVersion(version1+1);



                x += userMyMapper.update(userMy,new QueryWrapper<UserMy>().eq("id",userIndent.getMyuserid()).eq("version",version1));
                x += this.baseMapper.update(userIndent, new QueryWrapper<UserIndent>().eq("id", id).eq("version", version));

                if(x < 2){
                    throw new UnsupportedOperationException("失败");
                }
                return ResponseData.success();
            }
            userIndent.setStatus(Integer.parseInt(status));
            userIndent.setVersion(userIndent.getVersion()+1);
            UserMy userMy = userMyMapper.selectById(userIndent.getMyuserid());
            if(userMy == null){
                return ResponseData.error(500,"暂无此人");
            }

            VipPay vipPay = vipPayMapper.selectById(userIndent.getBackup3());
            if(vipPay == null){
                return ResponseData.error(500,"未找到会员信息");
            }

            Date date = DateUtils.addDateMinut(new Date(), (vipPay.getDay() * 24));
            userMy.setVipTime(date);
            userMy.setVipDuration(vipPay.getDay());
            userMy.setVipAddTime(new Date());
            userMy.setVipGrade(1);
            Integer version1 = userMy.getVersion();
            userMy.setVersion(version1+1);
            SysUser sysUser = sysUserMapper.selectById(userMy.getBackup6());
            if(sysUser == null){
                return ResponseData.error(500,"此人暂不存在");
            }

            sysUser.setVipId(1L);
            Long version2 = sysUser.getVersion();
            sysUser.setVersion(sysUser.getVersion()+1);
            x += sysUserMapper.update(sysUser,new QueryWrapper<SysUser>().eq("id",userMy.getBackup6()).eq("version",version2));
            x += userMyMapper.update(userMy,new QueryWrapper<UserMy>().eq("id",userIndent.getMyuserid()).eq("version",version1));
            x += this.baseMapper.update(userIndent, new QueryWrapper<UserIndent>().eq("id", id).eq("version", version));

            if(x < 3){
               throw new UnsupportedOperationException("失败");
            }
            return ResponseData.success();
        }else{
            return ResponseData.error(500,"请规范");
        }

        *//*Integer version = userIndent.getVersion();
        userIndent.setStatus(2);
        userIndent.setVersion(userIndent.getVersion()+1);
        int update = this.baseMapper.update(userIndent, new QueryWrapper<UserIndent>().eq("id", id).eq("version", version));*//*



        *//*if(update > 0){
            return ResponseData.success();
        }
        return ResponseData.error(500,"修改失败");*//*
    }*/


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData upStatus(String id,String status) {
        Integer x = 0;
        UserIndent userIndent = this.baseMapper.selectById(id);
        if(userIndent == null){
            return ResponseData.error(500,"暂无此条消息");
        }
        if(0 != userIndent.getStatus()){
            return ResponseData.error("您已经做出选择了");
        }
        Integer version = userIndent.getVersion();
        if("2".equals(status)){
            userIndent.setStatus(Integer.parseInt(status));
            userIndent.setVersion(userIndent.getVersion()+1);
            //贾儿子的
            x = this.baseMapper.update(userIndent,new QueryWrapper<UserIndent>().eq("id",id).eq("version",version));
            if(x > 0){
                return ResponseData.success();
            }
            return ResponseData.error(500,"刷新一下试试");
        }else if("1".equals(status)){
            userIndent.setStatus(Integer.parseInt(status));
            userIndent.setVersion(userIndent.getVersion()+1);
            UserMy userMy = userMyMapper.selectById(userIndent.getMyuserid());
            if(userMy == null){
                return ResponseData.error(500,"暂无此人");
            }

            VipPay vipPay = vipPayMapper.selectById(userIndent.getBackup3());
            if(vipPay == null){
                return ResponseData.error(500,"未找到会员信息");
            }

            Date date = DateUtils.addDateMinut(new Date(), (vipPay.getDay() * 24));
            userMy.setVipTime(date);
            userMy.setVipDuration(vipPay.getDay());
            userMy.setVipAddTime(new Date());
            userMy.setVipGrade(1);
            Integer version1 = userMy.getVersion();
            userMy.setVersion(version1+1);
            SysUser sysUser = sysUserMapper.selectById(userMy.getBackup6());
            if(sysUser == null){
                return ResponseData.error(500,"此人暂不存在");
            }

            sysUser.setVipId(1L);
            Long version2 = sysUser.getVersion();
            sysUser.setVersion(sysUser.getVersion()+1);
            x += sysUserMapper.update(sysUser,new QueryWrapper<SysUser>().eq("id",userMy.getBackup6()).eq("version",version2));
            x += userMyMapper.update(userMy,new QueryWrapper<UserMy>().eq("id",userIndent.getMyuserid()).eq("version",version1));
            x += this.baseMapper.update(userIndent, new QueryWrapper<UserIndent>().eq("id", id).eq("version", version));

            if(x < 3){
                throw new UnsupportedOperationException("失败");
            }
            return ResponseData.success();
        }else{
            return ResponseData.error(500,"请规范");
        }

        /*Integer version = userIndent.getVersion();
        userIndent.setStatus(2);
        userIndent.setVersion(userIndent.getVersion()+1);
        int update = this.baseMapper.update(userIndent, new QueryWrapper<UserIndent>().eq("id", id).eq("version", version));*/



        /*if(update > 0){
            return ResponseData.success();
        }
        return ResponseData.error(500,"修改失败");*/
    }


    private Serializable getKey(UserIndentParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserIndent getOldEntity(UserIndentParam param) {
        return this.getById(getKey(param));
    }

    private UserIndent getEntity(UserIndentParam param) {
        UserIndent entity = new UserIndent();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
